<?

/////////////// CLASSES /////////////

class CN {
	function CN($raw) {
		global $DB;
		
		$this->_addvars( is_string($raw) ? json_decode($raw): $raw);
		$this->raw = is_string($raw)? $raw: json_encode($raw);
		$this->table = $DB->type( $this->vars->type, "table" );
	}
	
	function _addvars($vars) { foreach($vars as $k=>$var) $this->$k = $var; }

		
	
	public $tablederivs	= array("page"=>"pages", "mod"=>"moddump", "text"=>"txtdump", "link"=>"db_link", "stream"=>"db_media", "media"=>"db_media", "group"=>"mod_object", "img"=>"mod_object", "newspost"=>"mod_news");
	public $sqlsugar	= array("img"=>"(SELECT sg.objid FROM glue_object as sg WHERE sg.parentype=%0 AND sg.objtype=%1 AND sg.parentid=%2) as img", "descr"=>"(SELECT st.long FROM txtdump as st WHERE st.textid=%0 AND st.lang='LANG' AND st.active=1) as descr");
	
	///////////////// EXECUTES /////////////////
	// Shift execute
	function shift() {
		global $DB;
		$meta = &$this->vars->meta;
		
		if(! $table = $this->tablederivs[$this->vars->type]) $table = "mod_".$this->vars->type;
		$value = ($meta->options)? $meta->options[(int)$meta->value] : (int)$meta->value;
		
		if(strpos($meta->field,".")) {
			$field = explode(".", $meta->field);
			$meta->field = array_shift($field);
			$jsonraw = ($this->vars->type == "mod")? $DB->query_direct( "select json from moddump where ID={$meta->objid}" ) : $DB->get_key( $this->vars->type, "ID={$meta->objid}", $meta->field ); 
			$json = json_decode(( $jsonraw )? $jsonraw : "{}" );
			foreach($field as $n => $fld) {
				if($n==count($field)-1) if(!$digger) $json->{$fld} = $value; else $digger->{$fld} = $value;
				else if($digger) $digger = &$digger->{$fld};
				else $digger = &$json->{$fld};
			}
			$value = json_encode($json);
		}
		$DB->query("UPDATE $table SET ".$meta->field."='$value' WHERE ID=".$meta->objid);
		return $meta->objid;
	}
	
	// Ajx field execute
	function ajax() {
		global $DB;
		$meta = &$this->vars->meta;
		
		//if(! $table = $this->tablederivs[$this->vars->type]) $table = "mod_".$this->vars->type;
		$value = ($meta->options)? $meta->options[(int)$meta->value] : $meta->value;

		
		if( strpos( $meta->field, "." )) {
			$field = explode( ".", $meta->field );
			$meta->field = array_shift( $field );
			$jsonraw = ($this->vars->type == "mod")? $DB->query_direct( "select json from moddump where ID={$meta->objid}" ) : $DB->get_key( $this->vars->type, "ID={$meta->objid}", $meta->field ); 
			$json = json_decode(( $jsonraw )? $jsonraw : "{}" );
			foreach($field as $n => $fld) {
				if($n==count($field)-1) if(!$digger) $json->{$fld} = $value; else $digger->{$fld} = $value;
				else if($digger) $digger = &$digger->{$fld};
				else $digger = &$json->{$fld};
			}
			$value = json_encode($json);
		}
		
		$DB->query( "UPDATE ". $DB->type( $this->vars->type, "table" ) ." SET ".$meta->field."='$value' WHERE ID=".$meta->objid, 0 );
		return $meta->objid;
	}
	
	
	
	// Pull execute
	function pull() {
		global $DB;
		$meta = &$this->vars->meta;
		
		
				
		if( $objects = $DB->filter( $this->vars->type, "active order by ID asc" ))
			foreach($objects as $k=>$obj)
				$objects[$k] = "{id:".$obj['ID'].", type:'".$this->vars->type."', tool:'".$this->vars->type.".basic', name:'".strlimit($obj[$meta->field], 20)."', descr:'', list:[\"".$obj[$meta->field]."\"], objtype:".$meta->objtype.", local:'".$this->vars->id."', meta:{}}";
			
		return ($objects)? implode(", ", $objects): 0;
	}
	
	function flow() {
		global $DB;
		if($this->vars->flow) $output = $this->vars->flow;
		unset($this->tmp->reqs);
		//if($this->vars->objtype) $this->tmp->reqs[] = (($this->vars->type=="calendar")? "ctype=":"type=") . $this->vars->objtype;
		if($req = $this->vars->reqs) foreach($req as $rk => $rv) $this->tmp->reqs[] = "$rk='$rv'";
		
		echo "/*";
		//echo $this->vars->type;
		echo "*/";
		
		$output->total = (int) $DB->get_key($this->vars->type, "ID>0", "count(*)" );
		if(!$output->current) $output->current = 0;
		if(!$output->limit) $output->limit = 20;
		if($this->action == "flow.prev") $output->current = (!$output->current && $output->limit < $output->total)? ($output->total - $output->limit) : (($output->current - $output->limit < 0)? 0: $output->current - $output->limit );
		if($this->action == "flow.next") $output->current = ($output->current + $output->limit < $output->total)? $output->current + $output->limit : 0;
		
		$this->vars->flow = $output;
		return json_encode($output); 
	}
	
	function parse_list() {
		global $DB, $_query_dummies;
		$flow = $this->flow();
		if($this->vars->flow->order) foreach($this->vars->flow->order as $key=>$val) $order[]= "$key $val";
		
		echo "/*";
		echo $this->vars->type, (($this->tmp->reqs)? implode(" and ", $this->tmp->reqs) : " ID>0") ." order by ".(($order)?implode(',', $order):"ID desc")." LIMIT ".$this->vars->flow->current.", ".$this->vars->flow->limit , $_query_dummies[$this->table][$this->action];
		echo "*/";
		
		if($objects = $DB->filter_key( $this->vars->type, (($this->tmp->reqs)? implode(" and ", $this->tmp->reqs) : " ID>0") ." order by ".(($order)?implode(',', $order):"ID desc")." LIMIT ".$this->vars->flow->current.", ".$this->vars->flow->limit , $_query_dummies[$this->table][$this->action] ) )
			foreach ($objects as $k=>$obj) $objects[$k] = array("id"=>$obj['ID'], "tool"=>$this->tool, "type"=>$this->vars->type, "name"=>$obj['short'], "descr"=>"", "list"=>array($obj["ID"], strlimit($obj['short'], 42), $obj['pdate']), "objtype"=>$this->vars->objtype );

		return json_encode(($objects)? $objects : array()) . ", $flow";
	}

}


















?>